# filter companies for blue-green deployment

def get_candidates(file_name:, total:)
  freq_by_tid = File.foreach(file_name).each_with_object({}) do |line, h|
    v, k = line.chomp.lstrip.split(" ").map(&:to_i)
    h[k] = v
  end

  ratio_by_tid = freq_by_tid.each_with_object({}) do |(k, v), h|
    h[k] = v.fdiv(total)
  end

  hash = ratio_by_tid.select { |_, v| v > 0.0001 }
  hash = hash.reject { |k, _| k == 0 }
  candidates = hash.reject { |k, _| block_ids.include?(k) }

  detailed_candidates = candidates.each_with_object({}) do |(tid, ratio), h|
    on_shard_by_company_id(tid) do
      com = Company.find(tid)
      h[tid] = {}
      h[tid]["subdomain"] = com.subdomain
      h[tid]["ratio"] = ratio * 100
      h[tid]["shard_id"] = ActiveRecord::Base.current_shard_id
      h[tid]["company_name"] = com.company_name
      h[tid]["agent_count"] = com.agents.count
      h[tid]["tid"] = tid
    end
  end

  de = detailed_candidates.select { |_, v| v['agent_count'] < 50 }
  de = de.select { |_, v| v['ratio'] > 0.02 }

  de.values.each do |h|
    puts "#{h['tid']},#{h['subdomain']},#{h['shard_id']},#{h['company_name']},#{h['ratio']},#{h['agent_count']}"
  end

  #de

  first_10_percent_tenants = select_first_10_percent(de)

  puts "First 10 percent:"
  print_list(first_10_percent_tenants)

  remain_40_percent = de.reject { |k, v| first_10_percent_tenants.map { |v| v['tid'] }.include?(k) }

  another_20_percent_tenants = select_20_percent(remain_40_percent)

  puts "another 20 percent:"
  print_list(another_20_percent_tenants)

  remain = remain_40_percent.reject { |k, v| another_20_percent_tenants.map { |v| v['tid'] }.include?(k) }.values
  puts "yet another 20 percent:"
  print_list(remain)
end


def get_ratio_of_key_ids(file_name:, total: )
  freq_by_tid = File.foreach(file_name).each_with_object({}) do |line, h|
    v, k = line.chomp.lstrip.split(" ").map(&:to_i)
    h[k] = v
  end

  ratio_by_tid = freq_by_tid.each_with_object({}) do |(k, v), h|
    h[k] = v.fdiv(total)
  end

  candidates = ratio_by_tid.select { |k, _| key_ids.include?(k) }

  detailed_candidates = candidates.each_with_object({}) do |(tid, ratio), h|
    on_shard_by_company_id(tid) do
      com = Company.find(tid)
      h[tid] = {}
      h[tid]["subdomain"] = com.subdomain
      h[tid]["ratio"] = ratio * 100
      h[tid]["shard_id"] = ActiveRecord::Base.current_shard_id
      h[tid]["company_name"] = com.company_name
      h[tid]["agent_count"] = com.agents.count
      h[tid]["tid"] = tid
    end
  end

  detailed_candidates.values.each do |h|
    puts "#{h['tid']},#{h['subdomain']},#{h['shard_id']},#{h['company_name']},#{h['ratio']},#{h['agent_count']}"
  end
end

def select_20_percent(h)
  results = []
  h.values.first(150).shuffle.each do |h|
    if results.inject(0) { |r, h| r + h["ratio"] } <= 20
      results << h
    end
  end
  results
end

def select_first_10_percent(de)
  results = []
  de.values.first(30).shuffle.each do |h|
    if results.inject(0) { |r, h| r + h["ratio"] } <= 10
      results << h
    end
  end
  results
end

def print_list(array)
  puts
  puts "----"

  array.each do |h|
    puts "#{h['tid']},#{h['subdomain']},#{h['shard_id']},#{h['company_name']},#{h['ratio']},#{h['agent_count']}"
  end

  puts "----"
  puts
end

def key_customer_subdomains
  %w(
    weimob
    mjweimob
    onion
    jnchun
    tsttst1
    tslsmart
    lining
    qxdaojia
    21cake
    xiaomai
    letote
    tongdun
    yakult
    qddfhr
    hlmkh
    xiaobang
    winit
    bigplus
    sci-techtown
    issc
    shebaomeng
    51shebao
    beisen
    youliwang
    wdmcake
    pohoocredit
    pkpm
    qianshengqian
    doweidu
    hla
    wkzuche
    navinfocal
    navinfo
    ciesco
    pkfare
    bitmain
    bdkj
    goodbaby
    haidilao
    chanjet
    lls
    danke
    yadea
    cifi01
    inspur
    huarunhuarun0
    jiediankeji
    star-services
    shell
    deli
    360cui
    wuxiapptec-hr
    wuxiapptec-hd
    wuxidiagnostics-csd
    labnetwork
    wuxiapptec-cou
    wuxiapptec-it
    enmonster
    wps
    pingpongx
    aikucun
    joyowo
    rcclchina
    TC
    cfca11
    xiaoe-tech
    KWG
    gq
    cmkj
    zhiliao
    xiaoyibao
    jushiwang
    manqiesi
    jcfc
    schneider-electric
    doterra
    yilian
    meiya
    hotelgg
    erongsheng
    51xf
    nrec
    assab
    ibaiqiu
    imoba
    hyxf
    htdkgroup
    zbrl
    nsfcxxzx
    cysq
    holiland
    hdlchina
    xhsd
    juzifenqi
    juzicallcenter
    kwm
    delta
    bosideng
    bosch-aa
    vandream
    hrccshanghai
    dongfangyuhong
    xdr
    starbuckschinapcc
    13570269686
    qiaohu
  )
end

def key_ids
  CompanyLocator.where(locator_type: 'subdomain', locator_content: key_customer_subdomains).pluck(:company_id)
end

def special_ids
  %w(
    1
    26
    51159
    5427
    6322
    20196).map(&:to_i)
end

def block_ids
  key_ids + special_ids
end

# get_candidates(file_name: "/tmp/freq.log", total: 17662172)
get_ratio_of_key_ids(file_name: "/tmp/freq.log", total: 17662172)
